home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / mtools.lha / mtools-2.0.7 / mren.c < prev    next >
C/C++ Source or Header  |  1992-09-10  |  3KB  |  119 lines

  1. /*
  2.  * Rename an existing MSDOS file
  3.  *
  4.  * Emmet P. Gray            US Army, HQ III Corps & Fort Hood
  5.  * ...!uunet!uiucuxc!fthood!egray    Attn: AFZF-DE-ENV
  6.  * fthood!egray@uxc.cso.uiuc.edu    Directorate of Engineering & Housing
  7.  *                     Environmental Management Office
  8.  *                     Fort Hood, TX 76544-5057
  9.  */
  10.  
  11. #include <stdio.h>
  12. #include "msdos.h"
  13. #include "patchlevel.h"
  14.  
  15. int fd = -1;                /* the file descriptor for the device */
  16. int dir_start;                /* starting sector for directory */
  17. int dir_len;                /* length of directory (in sectors) */
  18. int dir_entries;            /* number of directory entries */
  19. int clus_size;                /* cluster size (in sectors) */
  20. char *mcwd;                /* the Current Working Directory */
  21. int fat_error;                /* FAT error detected? */
  22.  
  23. main(argc, argv)
  24. int argc;
  25. char *argv[];
  26. {
  27.     int entry, ismatch, nogo, fargn, verbose;
  28.     char filename[13], *newfile, *strncpy(), *unix_name();
  29.     char new[13], ans[10], *temp, *strcpy(), drive, get_drive();
  30.     char *get_path(), *pathname, *get_name(), *fix_mcwd();
  31.     unsigned char *target, *dos_name();
  32.     void exit(), dir_write(), disk_flush(), dir_flush();
  33.     struct directory *dir, *dir_read();
  34.  
  35.     fargn = 1;
  36.     verbose = 0;
  37.     if (argc > 1) {
  38.         if (!strcmp(argv[1], "-v")) {
  39.             fargn = 2;
  40.             verbose = 1;
  41.         }
  42.     }
  43.     if (argc != fargn + 2) {
  44.         fprintf(stderr, "Mtools version %s, dated %s\n", VERSION, DATE);
  45.         fprintf(stderr, "Usage: %s [-v] sourcefile targetfile\n", argv[0]);
  46.         exit(1);
  47.     }
  48.     mcwd = fix_mcwd();
  49.  
  50.     drive = get_drive(argv[1]);
  51.     if (init(drive, 2)) {
  52.         fprintf(stderr, "%s: Cannot initialize '%c:'\n", argv[0], drive);
  53.         exit(1);
  54.     }
  55.     strcpy(filename, get_name(argv[fargn]));
  56.     pathname = get_path(argv[fargn]);
  57.     if (subdir(drive, pathname))
  58.         exit(1);
  59.  
  60.     temp = get_name(argv[fargn + 1]);
  61.     target = dos_name(argv[fargn + 1], verbose);
  62.  
  63.     strcpy(new, unix_name(target, target + 8));
  64.     nogo = 0;
  65.                     /* the name supplied may be altered */
  66.     if (strcmp(temp, new) && verbose) {
  67.         while (!nogo) {
  68.             printf("Do you accept \"%s\" as the new filename (y/n) ? ", new);
  69.             gets(ans);
  70.             if (ans[0] == 'y' || ans[0] == 'Y')
  71.                 break;
  72.             if (ans[0] == 'n' || ans[0] == 'N')
  73.                 nogo = 1;
  74.         }
  75.     }
  76.     if (nogo)
  77.         exit(0);
  78.                     /* see if exists and do it */
  79.     ismatch = 0;
  80.     for (entry = 0; entry < dir_entries; entry++) {
  81.         dir = dir_read(entry);
  82.                     /* if empty */
  83.         if (dir->name[0] == 0x0)
  84.             break;
  85.                     /* if erased */
  86.         if (dir->name[0] == 0xe5)
  87.             continue;
  88.                     /* if volume label */
  89.         if ((dir->attr & 0x08))
  90.             continue;
  91.                     /* you may rename a directory */
  92.         newfile = unix_name(dir->name, dir->ext);
  93.  
  94.                     /* if the new name already exists */
  95.         if (!strcmp(new, newfile)) {
  96.             fprintf(stderr, "%s: File \"%s\" already exists\n", argv[0], new);
  97.             exit(1);
  98.         }
  99.                     /* if the old name exists */
  100.         if (match(newfile, filename)) {
  101.             ismatch = 1;
  102.             break;
  103.         }
  104.     }
  105.     if (!ismatch) {
  106.         fprintf(stderr, "%s: File \"%s\" not found\n", argv[0], filename);
  107.         exit(1);
  108.     }
  109.                     /* so go ahead and do it */
  110.     strncpy((char *) dir->name, (char *) target, 8);
  111.     strncpy((char *) dir->ext, (char *) target + 8, 3);
  112.     dir_write(entry, dir);
  113.  
  114.     dir_flush();
  115.     disk_flush();
  116.     close(fd);
  117.     exit(0);
  118. }
  119.